package chapter1;

import java.io.*;
import java.util.*;

class Main implements Runnable{
    static String ReadLn(int maxLength){  
        byte line[] = new byte [maxLength];
        int length = 0;
        int input = -1;
        try{
            while (length < maxLength){
                input = System.in.read();
                if ((input < 0) || (input == '\n')) break; 
                line [length++] += input;
            }

            if ((input < 0) && (length == 0)) return null;  
            return new String(line, 0, length);
        }catch (IOException e){
            return null;
        }
    }

    public static void main(String args[])  
    {
        Main myWork = new Main();  
        myWork.run();            
    }

    public void run() {
        new myStuff().run();
    }
}

class myStuff implements Runnable{
    public void run()
    {
    	String input;
    	StringTokenizer idata;
    	int n,m, count;
    	String k;
    	char [][] result;
    	int num = 0;
    	
    	while ((input = Main.ReadLn(255)) != null)
        {	
    	 	idata = new StringTokenizer(input);
    	 	
    		n = Integer.parseInt(idata.nextToken());
        	m = Integer.parseInt(idata.nextToken());
        
			if (n != 0 && m != 0) 
			{
			result = new char[n][m];
			count = 0;
			
			if (num > 0)
			{
				System.out.println();
			}
			
			while (count < n)
			{
				input = Main.ReadLn(255);
				
				idata = new StringTokenizer(input);
				
				k = idata.nextToken();

				for (int j = 0; j < m; j++) 
				{
					char value = k.charAt(j);
					result[count][j] = value;
				}

				count++;
					
				if (count == n) 
				{	
					num++;
					
					//a[num - 1] = new Stuff(result, n, m, num);
					
					mineSweeper(n, m, num, result);
				}
			}
			} else {
				break;
			}
    	}
    	//MineSweeper(a, num);
    	//System.out.println();
    }
   
	void mineSweeper(int rows, int columns, int count, char[][]line)
	{
		for (int i = 0; i < rows; i++)
		{
			for (int j = 0; j < columns; j++)
			{
				// Check if index has a mine - i.e '*'.
				if (line[i][j] == '*')
				{
					// Retrieve positions surrounding the mine.
					int north = i - 1;
					int south = i + 1;
					int east = j + 1;
					int west = j - 1;
					 
							
					if (north > - 1)
					{
						if (line[north][j] != '*')
						{
							int value = Character.getNumericValue(line[north][j]);
							
							if (value == -1)
							{
								value = 0;
							}
							
							line[north][j] = Character.forDigit(value + 1, 10);	
						}
					}
					
					if (south < rows)
					{
						if (line[south][j] != '*')
						{
							int value = Character.getNumericValue(line[south][j]);
							
							if (value == -1)
							{
								value = 0;
							}
							
							line[south][j] = Character.forDigit(value + 1, 10);
						}
					}
					
					if (east < columns)
					{
						if (line[i][east] != '*')
						{
							int value = Character.getNumericValue(line[i][east]);
							
							if (value == -1)
							{
								value = 0;
							}
							
							line[i][east] = Character.forDigit(value + 1, 10);	
						}
					}
					
					if (west > -1)
					{
						if (line[i][west] != '*')
						{
							int value = Character.getNumericValue(line[i][west]);
							
							if (value == -1)
							{
								value = 0;
							}
							
							line[i][west] = Character.forDigit(value + 1, 10);
						}
					}
					
					// top left diagonal.
					if (west > -1 && north > - 1)
					{
						if (line[north][west] != '*')
						{
							int value = Character.getNumericValue(line[north][west]);
							
							if (value == -1)
							{
								value = 0;
							}
							
						line[north][west] = Character.forDigit(value + 1, 10);
						}
				
					}
					
					// top right diagonal.
					if (east < columns && north > -1)
					{
						int value = Character.getNumericValue(line[north][east]);
						
						if (line[north][east] != '*')
						{
							if (value == -1)
							{
								value = 0;
							}
							
							line[north][east] = Character.forDigit(value + 1, 10);
						}
					
					}
						
					// bottom left diagonal,
					if (west > -1 && south < rows)
					{
						int value = Character.getNumericValue(line[south][west]);
						
						if (line[south][west] != '*')
						{
							if (value == -1)
							{
								value = 0;
							}
							
							line[south][west] = Character.forDigit(value + 1, 10);
						}
					}
					
					// bottom right diagonal.
					if (east < columns && south < rows)
					{
						int value = Character.getNumericValue(line[south][east]);
						
						if (line[south][east] != '*')
						{
							if (value == -1)
							{
								value = 0;
							}
							
							line[south][east] = Character.forDigit(value + 1, 10);
						}
					}
					
				} else if (line[i][j] == '.') {
					line[i][j] = '0';
				}
			}
		}
		
		printResults(rows, columns, count, line);
	}
	
	void printResults(int n, int m, int num, char[][]result)
	{
		System.out.println("Field #" + num + ":");
		
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				System.out.print(result[i][j]);
			}
			System.out.println();
		}
	}
}
